package com.lz.fruitday.fragment;


import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.lz.fruitday.R;
import com.lz.fruitday.adapter.KindGvAdapter;
import com.lz.fruitday.adapter.KindLvAdapter;
import com.lz.fruitday.bean.CommonBean;
import com.lz.fruitday.bean.KindBaseBean;
import com.lz.fruitday.bean.SubBean;
import com.lz.fruitday.constant.Path;
import com.lz.fruitday.widget.GridViewForScrollView;

import java.util.ArrayList;

import cz.msebera.android.httpclient.Header;

/**
 * A simple {@link Fragment} subclass.
 */
public class KindFragment extends Fragment {

    @ViewInject(R.id.lv_kind)
    private ListView lvKind;
    private ArrayList<CommonBean> commons = new ArrayList<CommonBean>();
    private KindLvAdapter lvAdapter;
    private AsyncHttpClient client = new AsyncHttpClient();
    @ViewInject(R.id.tv_common_name)
    private TextView tvName;
    @ViewInject(R.id.gv_kind)
    private GridViewForScrollView gvKind;
    private ArrayList<SubBean> subBeans = new ArrayList<>();
    private KindGvAdapter gvAdapter;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_kind, container, false);
        ViewUtils.inject(this, view);
        initView();
        initData();
        return view;
    }

    private void initData() {
        client.post(Path.PATH_KIND, getParams(), new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                TypeReference<KindBaseBean<CommonBean>> typeReference = new TypeReference<KindBaseBean<CommonBean>>() {
                };
                KindBaseBean baseBean = JSONObject.parseObject(responseString, typeReference);
                commons.addAll(baseBean.getCommon());
                lvAdapter.notifyDataSetChanged();

                tvName.setText(commons.get(0).getName());
                subBeans.addAll(commons.get(0).getSub_level());
                gvAdapter.notifyDataSetChanged();
                lvKind.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        subBeans.clear();
                        tvName.setText(commons.get(position).getName());
                        subBeans.addAll(commons.get(position).getSub_level());
                        gvAdapter.notifyDataSetChanged();

                    }
                });

            }
        });
    }

    private RequestParams getParams() {
        RequestParams params = new RequestParams();
        params.add("sign", "c316fe3aee275e34712eea2cdaa92d70");
        params.add("timestamp", "1438873421");
        params.add("platform", "ANDROID");
        params.add("source", "app");
        params.add("connect_id", "");
        params.add("region_id", "143949");
        params.add("service", "product.getCatList");
        params.add("device_id", "93789e8978f2eefdcf52006b4d96ee23");
        params.add("channel", "qq");
        params.add("version", "2.3.0");
        return params;
    }

    private void initView() {
        lvAdapter = new KindLvAdapter(commons, getActivity());
        lvKind.setAdapter(lvAdapter);

        gvAdapter = new KindGvAdapter(subBeans, getActivity());
        gvKind.setAdapter(gvAdapter);
    }


}
